﻿using Dapper;
using System;
using BCEntity.TMS.WorryFreeMoveSuiteConfigTemplate;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.WorryFreeMoveSuiteConfigTemplate
{
	/// <summary>
	/// 无忧搬家套餐配置模板
	/// </summary>
	public class WorryFreeMoveSuiteConfigTemplateData : IWorryFreeMoveSuiteConfigTemplateData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM worryfreemovesuiteconfigtemplate";
		private const string QUERY = @"SELECT A.WorryFreeMoveSuiteConfigTemplateId,A.CarryServiceRemark,A.Enabled,A.CreateAdminId,A.CreateAdminName,A.CreateTime,A.UpdateTime,A.EditAdminId,A.EditAdminName,
											A.SuiteName,A.VehicleBusinessMappingId,A.VehicleTypeId,A.SuiteForCrowdTitle,A.SuiteForCrowdRemark,A.SuiteServiceTitle,A.SuiteServiceRemark,A.CarryServiceAmount,
											B.TransportBusiness,C.VehicleTypeName,C.VehicleTypeEnum,C.VehiclePicPath
										FROM worryfreemovesuiteconfigtemplate A
										INNER JOIN VehicleBusinessMapping B ON A.VehicleBusinessMappingId=B.VehicleBusinessMappingId
										INNER JOIN VehicleType C ON B.VehicleTypeId=C.VehicleTypeId ";
		public WorryFreeMoveSuiteConfigTemplateData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查无忧搬家套餐配置模板是否存在
		public async Task<bool> Exists(int worryFreeMoveSuiteConfigTemplateId)
		{
			string sql = string.Format(@"{0} WHERE WorryFreeMoveSuiteConfigTemplateId=?WorryFreeMoveSuiteConfigTemplateId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigTemplateId", worryFreeMoveSuiteConfigTemplateId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		public async Task<bool> Exists(int vehicleBusinessMappingId, string suiteName)
		{
			string sql = string.Format(@"{0} WHERE VehicleBusinessMappingId=?VehicleBusinessMappingId AND SuiteName=?SuiteName LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?SuiteName", suiteName, DbType.AnsiString);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加无忧搬家套餐配置模板
		public async Task<WorryFreeMoveSuiteConfigTemplateEntity> Add(WorryFreeMoveSuiteConfigTemplateEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("worryfreemovesuiteconfigtemplate", new string[] { "CarryServiceRemark", "CreateAdminId", "CreateAdminName", "SuiteName", 
				"VehicleBusinessMappingId", "VehicleTypeId", "SuiteForCrowdTitle", "SuiteForCrowdRemark", "SuiteServiceTitle", "SuiteServiceRemark", "CarryServiceAmount" });
			sql = string.Format("{0} {1} WHERE A.WorryFreeMoveSuiteConfigTemplateId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?CarryServiceRemark", entity.CarryServiceRemark?.Trim(), DbType.String);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?SuiteName", entity.SuiteName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleBusinessMappingId", entity.VehicleBusinessMappingId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?SuiteForCrowdTitle", entity.SuiteForCrowdTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteForCrowdRemark", entity.SuiteForCrowdRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceTitle", entity.SuiteServiceTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceRemark", entity.SuiteServiceRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CarryServiceAmount", entity.CarryServiceAmount, DbType.Decimal, size: 10);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigTemplateEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改无忧搬家套餐配置模板
		public async Task<WorryFreeMoveSuiteConfigTemplateEntity> Update(WorryFreeMoveSuiteConfigTemplateEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("worryfreemovesuiteconfigtemplate", new string[] { "CarryServiceRemark", "EditAdminId", "EditAdminName", "SuiteName",
				"VehicleBusinessMappingId", "VehicleTypeId", "SuiteForCrowdTitle", "SuiteForCrowdRemark", "SuiteServiceTitle", "SuiteServiceRemark", "CarryServiceAmount" 
				}, new string[] { "WorryFreeMoveSuiteConfigTemplateId" });
			sql = string.Format("{0} {1} WHERE A.WorryFreeMoveSuiteConfigTemplateId=?WorryFreeMoveSuiteConfigTemplateId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigTemplateId", entity.WorryFreeMoveSuiteConfigTemplateId, DbType.Int32, size: 11);
			parameters.Add("?CarryServiceRemark", entity.CarryServiceRemark?.Trim(), DbType.String);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?SuiteName", entity.SuiteName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleBusinessMappingId", entity.VehicleBusinessMappingId, DbType.Int32, size: 11);
			parameters.Add("?VehicleTypeId", entity.VehicleTypeId, DbType.Int32, size: 11);
			parameters.Add("?SuiteForCrowdTitle", entity.SuiteForCrowdTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteForCrowdRemark", entity.SuiteForCrowdRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceTitle", entity.SuiteServiceTitle?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?SuiteServiceRemark", entity.SuiteServiceRemark?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CarryServiceAmount", entity.CarryServiceAmount, DbType.Decimal, size: 10);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigTemplateEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除无忧搬家套餐配置模板
		public async Task<bool> Delete(int worryFreeMoveSuiteConfigTemplateId)
		{
			string sql = @"DELETE FROM worryfreemovesuiteconfigtemplate WHERE WorryFreeMoveSuiteConfigTemplateId=?WorryFreeMoveSuiteConfigTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigTemplateId", worryFreeMoveSuiteConfigTemplateId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取无忧搬家套餐配置模板
		public async Task<WorryFreeMoveSuiteConfigTemplateEntity> GetEntity(int worryFreeMoveSuiteConfigTemplateId)
		{
			string sql = string.Format(@"{0} WHERE A.WorryFreeMoveSuiteConfigTemplateId=?WorryFreeMoveSuiteConfigTemplateId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigTemplateId", worryFreeMoveSuiteConfigTemplateId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigTemplateEntity>(sql, parameters);
		}
		public async Task<WorryFreeMoveSuiteConfigTemplateEntity> GetEntity(int vehicleBusinessMappingId, string suiteName)
		{
			string sql = string.Format(@"{0} WHERE A.VehicleBusinessMappingId=?VehicleBusinessMappingId AND A.SuiteName=?SuiteName LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleBusinessMappingId", vehicleBusinessMappingId, DbType.Int32);
			parameters.Add("?SuiteName", suiteName, DbType.AnsiString);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorryFreeMoveSuiteConfigTemplateEntity>(sql, parameters);
		}
		#endregion

		#region 获取无忧搬家套餐配置模板列表
		public async Task<IEnumerable<WorryFreeMoveSuiteConfigTemplateEntity>> GetList()
		{
			string sql = string.Format(@"{0} WHERE A.Enabled=1", QUERY);
			return await this.databaseContext.Master.QueryAsync<WorryFreeMoveSuiteConfigTemplateEntity>(sql);
		}
		#endregion

		#region 获取无忧搬家套餐配置模板分页
		public async Task<(IEnumerable<WorryFreeMoveSuiteConfigTemplateEntity>, int)> Query(string suiteName,bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
			if (!string.IsNullOrWhiteSpace(suiteName))
			{
				wheres.Add("A.SuiteName LIKE CONCAT('%',CONCAT(?SuiteName,'%'))");
				parameters.Add("?SuiteName", suiteName, DbType.AnsiString);
			}
			if (enabled.HasValue)
			{
				wheres.Add("A.Enabled=?Enabled");
				parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM worryfreemovesuiteconfigtemplate A 
							INNER JOIN VehicleBusinessMapping B ON A.VehicleBusinessMappingId=B.VehicleBusinessMappingId
							INNER JOIN VehicleType C ON B.VehicleTypeId=C.VehicleTypeId {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "A.CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<WorryFreeMoveSuiteConfigTemplateEntity, int>(sql, parameters);
		}
		#endregion

		#region 启用无忧搬家套餐配置模板
		public async Task<bool> Enabled(int worryFreeMoveSuiteConfigTemplateId, long adminId, string fullname)
		{
			string sql = @"UPDATE worryfreemovesuiteconfigtemplate SET Enabled=1,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE WorryFreeMoveSuiteConfigTemplateId=?WorryFreeMoveSuiteConfigTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigTemplateId", worryFreeMoveSuiteConfigTemplateId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 禁用无忧搬家套餐配置模板
		public async Task<bool> Disabled(int worryFreeMoveSuiteConfigTemplateId, long adminId, string fullname)
		{
			string sql = @"UPDATE worryfreemovesuiteconfigtemplate SET Enabled=0,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE WorryFreeMoveSuiteConfigTemplateId=?WorryFreeMoveSuiteConfigTemplateId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorryFreeMoveSuiteConfigTemplateId", worryFreeMoveSuiteConfigTemplateId, DbType.Int32);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion
	}
}



